/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsNumber extends LoggedDataPanels implements ActionListener , KeyListener{

    public LoggedDataPanelsNumber() {
        super();
    }

    private boolean hasLowerLimit;
    private long lowerLimit;
    private boolean hasUpperLimit;
    private long upperLimit;

    public LoggedDataPanelsNumber(boolean hasLowerLimit, long lowerLimit, boolean hasUpperLimit, long upperLimit) {
        this.hasLowerLimit = hasLowerLimit;
        this.lowerLimit = lowerLimit;
        this.hasUpperLimit = hasUpperLimit;
        this.upperLimit = upperLimit;
    }

    
    private static final int MIN_SIZE=5;
    
    @Override
    public Component getViewComponent() {
        validateDataToDisplay();
        JLabel lblViewData=new JLabel();

        if (getData().value().equals("")) {
            lblViewData.setText("");
            
        } else {
            long value=Long.valueOf(getData().value());
            lblViewData.setText(Long.toString(value));
        }
        return(lblViewData);
    }

    JTextField fldEditText;

    @Override
    public Component getEditComponent() {
        JPanel pnlComponent=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        fldEditText=new JTextField();
        fldEditText.addActionListener(this);
        fldEditText.addKeyListener(this);
        if (hasDataInitialized()) {
            if (getData().value().length()<MIN_SIZE) {
                fldEditText.setColumns(MIN_SIZE);
            }
            fldEditText.setText(getData().value());
        } else {
            fldEditText.setColumns(MIN_SIZE);
            fldEditText.setText(getDefaultValue());
        }
        pnlComponent.add(fldEditText);
        return(pnlComponent);
    }

    @Override
    public String getEdittedValue() {
        return(fldEditText.getText());
    }

    public boolean isValid() {
        boolean allOkay=true;
        String currentText=fldEditText.getText();
        if (!currentText.equals("")) {
            long currentNumber=0;
            try {
                currentNumber=Long.valueOf(currentText);
            } catch (NumberFormatException ex) {
                allOkay=false;
            }
            if (hasLowerLimit) {
                if (currentNumber<lowerLimit) {
                    allOkay=false;
                }
            }
            if (hasUpperLimit) {
                if (currentNumber>upperLimit) {
                    allOkay=false;
                }
            }
        }
        return(allOkay);
    }
    
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        // sends both valid and invalid events
        ChangeEvent eNew=new ChangeEvent(e.getSource());
        notifyAllChangeListeners(eNew);
    }

    @Override
    public boolean isEmpty() {
        return(fldEditText.getText().equals(""));
    }

    @Override
    public void keyTyped(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }

    @Override
    public void keyPressed(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }

    @Override
    public void keyReleased(KeyEvent e) {
        actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Key"));
    }
    
}
